//
//  VCAllOrder.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCOpenMember: VCBase {
    
    // 布局
    private lazy var openMemberView:OpenMemberView = {
        let ui = OpenMemberView()
        ui.backgroundColor = UIColor.white
        let uiY = realHeight + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        init_date()
        init_event()
    }
}

extension VCOpenMember {
    
    func openMember(request:NSDictionary) -> Void {
        // 请求
        self.doPost(api: "modify/user", dict: request) { (dict) in
            self.show(text: "开通成功")
            UserDefaults.standard.set(2, forKey: SP_PERMISSIONID)
            // 解析数据
            guard let result = dict.value(forKey: SP_BODYREQUEST) as? NSDictionary else {
                return
            }
            let userInfo = UserInfo(dict: result as! [String : Any])
            self.openMemberView.userInfo = userInfo
        }
    }
    
    private func init_event() -> Void {
        
        openMemberView.complete = {money,text in
            // 解析数据
            guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
                self.show(text: "本地数据获取失败，请使用账号密码重新登录")
                return
            }
            // 拼接数据
            let request = NSMutableDictionary()
            request.setValue(userid, forKey: SP_USERID)
            if text.elementsEqual("关闭会员") {
                request.setValue("10", forKey: "openmoney")
            }else {
                request.setValue(money, forKey: "openmoney")
            }
            // 请求
            self.doPost(api: "modify/user", dict: request) { (dict) in
                self.show(text: "成功")
                if text.elementsEqual("关闭会员") {
                    UserDefaults.standard.set(1, forKey: SP_PERMISSIONID)
                }else {
                    UserDefaults.standard.set(2, forKey: SP_PERMISSIONID)
                }
                // 解析数据
                guard let result = dict.value(forKey: SP_BODYREQUEST) as? NSDictionary else {
                    return
                }
                let userInfo = UserInfo(dict: result as! [String : Any])
                self.openMemberView.userInfo = userInfo
            }
        }
    }
    
    private func init_date() -> Void {
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(userid, forKey: SP_USERID)
        // 请求
        self.doPost(api: "search/user", dict: request) { (dict) in
            // 解析数据
            guard let result = dict.value(forKey: SP_BODYREQUEST) as? NSDictionary else {
                return
            }
            let userInfo = UserInfo(dict: result as! [String : Any])
            self.openMemberView.userInfo = userInfo
            // 校验用户会员是否过期
            if userInfo.expiredate != nil ,self.dateInterval(start: userInfo.serveredate, end: userInfo.expiredate) {
                UserDefaults.standard.setValue(3, forKey: SP_PERMISSIONID)
            }
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(openMemberView)
    }
}
